﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileContactTypeActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile contact type actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Profiles
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profile contact type actioner.
    /// </summary>
    public class ProfileContactTypeActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileContactTypeActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ProfileContactTypeActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create profile contact type.
        /// </summary>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateProfileContactType(string type)
        {
            var contacttype = new ProfileContactType();
            contacttype.ID = Guid.NewGuid();
            contacttype.Type = type;
            contacttype.CreatedDateTime = DateTime.Now;
            this.Session.Save(contacttype);

            this.Log.CreateLogEntry(
                contacttype.ID, 
                string.Format("Kontakt type '{0}', blev oprettet.", contacttype.Type), 
                Enums.LogEntryType.ProfileContactType);

            return contacttype.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileContactTypesResult"/>.
        /// </returns>
        public ProfileContactTypesResult ExecuteSearchQuery(ProfileContactTypesQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new ProfileContactTypesResult();

            var search = this.Session.QueryOver<ProfileContactType>();
            this.ApplyDefaultCriterias(settings, search);

            int rowCount = search.RowCount();
            var relations = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Type).Asc);

            result.TotalRowCount = rowCount;

            foreach (ProfileContactType profileContactType in relations)
            {
                Services.Dto.ProfileContactType dto = Services.Dto.ProfileContactType.Convert(profileContactType);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get profile contact type.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileContactType"/>.
        /// </returns>
        public ProfileContactType GetProfileContactType(Guid id)
        {
            var rel = this.Session.Get<ProfileContactType>(id);

            return rel;
        }

        #endregion
    }
}